package com.zsk.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

/**
 * 图像覆盖修复
 * 
 * @author zincredible
 * @date 2019/07/18 10:09:48
 */
public class UITest extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton button;
	private BufferedImage image;

	private ExecutorService threadPool;

	public void init() {
		setSize(600, 800);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		button = new JButton("保存图片");
		addSaveImageLisentner(button);
		add(button, BorderLayout.NORTH);
		generateImage();
		setVisible(true);

		threadPool = Executors.newSingleThreadExecutor();
	}

	private void addSaveImageLisentner(JButton btn) {
		btn.addActionListener(e -> {
			threadPool.execute(() -> System.out.println(System.currentTimeMillis()));
		});
//		btn.addActionListener(e -> {
//			JFileChooser fileChooser = new JFileChooser("D:\\");
//			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//			int returnVal = fileChooser.showOpenDialog(fileChooser);
//			if (returnVal == JFileChooser.APPROVE_OPTION) {
//				String filePath = fileChooser.getSelectedFile().getAbsolutePath();
//				try {
//					ImageIO.write(image, "jpg", new File(filePath + "\\" + System.currentTimeMillis() + ".jpg"));
//					JOptionPane.showMessageDialog(this, "图片保存成功");
//				} catch (IOException e1) {
//					e1.printStackTrace();
//				}
//			}
//
//		});
	}

	private void generateImage() {
		try {
			image = ImageIO.read(new File("C:\\Users\\ZC20181120-1\\Desktop\\123.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		image.createGraphics();
		Graphics2D g = (Graphics2D) image.getGraphics();
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int imageHeigh = image.getHeight();
		int imageWight = image.getWidth();
		System.out.println(imageHeigh + "," + imageWight);

		int x = 10;
		int y = 10;
		for (int i = 0; i < imageHeigh; i++) {
			if (i >= 180 && i <= 250) {
				for (int j = 0; j < imageWight; j++) {
					if (j >= 40 && j <= 80) {
						// int[] rgb = getRGB(image, x, y);
						image.setRGB(i, j, image.getRGB(x, y));
						// g.setColor(new Color(rgb[0], rgb[1], rgb[2]));
						// g.drawLine(i, j, i, j);
						y++;
					}
				}
				x++;
				y = 10;
			}

		}
		g.setColor(Color.BLACK);
		g.setFont(new Font("新宋体", Font.PLAIN, 18));
		g.drawString("证明-张思科", 200, 55);
		JLabel label = new JLabel();
		Icon icon = new ImageIcon(image);
		label.setIcon(icon);
		JScrollPane scroolPicLeft = new JScrollPane(label);
		add(scroolPicLeft, BorderLayout.CENTER);

	}

	/**
	 * 获取[x,y]区间随机数
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public int getRandom(int x, int y) {
		int num = 0;
		// 说明：两个数在合法范围内，并不限制输入的数哪个更大一些
		if (x < 0 || y < 0) {
			return num;
		} else {
			int max = x > y ? x : y;
			int min = x < y ? x : y;
			int mid = max - min;// 求差
			// 产生随机数
			num = (int) (Math.random() * (mid + 1)) + min;
		}
		return num;
	}

	/**
	 * 获取某个位置像素点的RGB
	 * 
	 * @param image
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] getRGB(BufferedImage image, int x, int y) {
		int[] rgb = new int[3];
		int pixel = image.getRGB(x, y); // 下面三行代码将一个数字转换为RGB数字
		rgb[0] = (pixel & 0xff0000) >> 16;
		rgb[1] = (pixel & 0xff00) >> 8;
		rgb[2] = (pixel & 0xff);
		return rgb;
	}

	public static void main(String[] args) {
		new UITest().init();
	}
}
